//
//  BJBaseViewController.m
//  BaoJunNewEnergy
//
//  Created by 9s on 2018/3/15.
//  Copyright © 2018年 SIXECO. All rights reserved.
//

#import "BJBaseViewController.h"
@interface BJBaseViewController ()
@property(nonatomic,strong)CALayer *separatorLayer;
@end

@implementation BJBaseViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    if (self.navigationController.viewControllers.count==1) {
        [self.tabBarController setHidesBottomBarWhenPushed:NO];
    }
    else
    {
        [self.tabBarController setHidesBottomBarWhenPushed:YES];
    }
}
-(CALayer *)separatorLayer
{
    if (!_separatorLayer) {
        _separatorLayer = [CALayer layer];
        _separatorLayer.frame = CGRectMake(0, KNavHeight-0.5, ScreenWidth, 0.5);
        _separatorLayer.backgroundColor = RGB(223,223, 223).CGColor;
    }
    return _separatorLayer;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    //self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.jz_fullScreenInteractivePopGestureEnabled = YES;
    self.jz_navigationBarTintColor = [UIColor redColor];
    if (self.parentViewController.childViewControllers.count>1) {
        [self setLeftButton:nil Image:nil];
    }
    else{
        if (self.presentingViewController) {
            [self setLeftButton:nil Image:@"nav_back"];
        }
    }

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.titleTextAttributes = [self getNavigationTitleTextAttributes];
}
//导航栏左边按钮
-(void)setLeftButton:(NSString *)title Image:(NSString *)image{
    UIButton *btnBack       = [[UIButton alloc]init];
    btnBack.backgroundColor = [UIColor clearColor];
    btnBack.frame           = CGRectMake(0, 0, 60, 44);
    [btnBack setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];//UIControlContentHorizontalAlignmentCenter
    if (title) {
        btnBack.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        [btnBack setTitle:title forState:UIControlStateNormal];
        btnBack.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        [btnBack setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    else{
        //btnBack.imageEdgeInsets=UIEdgeInsetsMake(0, -15, 0, 0);
        btnBack.imageEdgeInsets=UIEdgeInsetsMake(0, 0, 0, 0);
        
        if (image) {
            [btnBack setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
        }
        else{
            [btnBack setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
        }
    }
    [btnBack addTarget:self action:@selector(leftReturn:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:btnBack];
    
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    
    if (IOS11) {
        negativeSpacer.width = -15;
    }else{
        negativeSpacer.width = 0;
    }
    self.navigationItem.leftBarButtonItems = @[negativeSpacer, backItem];
}

//导航栏左边按钮事件
-(void)leftReturn:(UIButton *)sender
{
    if (![self isMemberOfClass:[BJBaseViewController class]])
    {
        if (self.parentViewController.childViewControllers.count>1) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        else{
            if (self.presentingViewController) {
                [self dismissViewControllerAnimated:YES completion:nil];
                
            }
        }
    }
}

- (NSDictionary*) getNavigationTitleTextAttributes
{
    return @{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:FONT_SIZE(17)};
}

#pragma mark - nav buttonitem
- (void)setLeftButton:(NSString*)buttonTitle titleImage:(NSString *)TitleImage action:(SEL)action
{
    //CGSize size = [buttonTitle sizeWithFont:[UIFont boldSystemFontOfSize:15]];
    CGSize size = [buttonTitle sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    UIImage* image = [UIImage imageNamed:TitleImage];
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [button setFrame:CGRectMake(0, 0, MAX((size.width+30),40), MAX(30,size.height))];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:[image stretchableImageWithLeftCapWidth:20 topCapHeight:15] forState:UIControlStateNormal];
    [button setTitle:buttonTitle forState:UIControlStateNormal];
    [button setTitle:buttonTitle forState:UIControlStateHighlighted];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -10;
    self.navigationItem.leftBarButtonItems = @[negativeSpacer, backItem];
}
- (UIButton *)setRightButton:(NSString*)buttonTitle titleImage:(NSString *)TitleImage action:(SEL)action
{
    //CGSize size = [buttonTitle sizeWithFont:[UIFont boldSystemFontOfSize:15]];
    CGSize size = [buttonTitle sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    UIImage* image = [UIImage imageNamed:TitleImage];
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [button setFrame:CGRectMake(0, 0, MAX((size.width+30),40), MAX(30,size.height))];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:[image stretchableImageWithLeftCapWidth:20 topCapHeight:15] forState:UIControlStateNormal];
    [button setTitle:buttonTitle forState:UIControlStateNormal];
    [button setTitle:buttonTitle forState:UIControlStateHighlighted];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    
    
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -10;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer, backItem];
    return button;
}

- (void)setNavigationBarBackGroundWithImage:(UIImage *)image{
    [self.navigationController.navigationBar setBackgroundImage:image forBarPosition:UIBarPositionAny
                                                     barMetrics:UIBarMetricsDefault];
}

- (void)setNavigationBarBackGroundWithColor:(UIColor *)color{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:color] forBarPosition:UIBarPositionAny
                                                     barMetrics:UIBarMetricsDefault];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
